/**
 * auth: zhukaijun
 * date: 2022-5-8
 * des: stompjs rabbitmq event msg handler
 */
import Stomp from "stompjs";
import emitter from "./emitter";

export default {
  newInstance(v) {
    return this.instance = (obj => {
      return obj || {
        subList: {},
        connectStatus: 0,
        msgEvtPrefix: "ws-subscribe-msg-",
        disconnect() {
          try {
            this.client.disconnect(() => {
            });
          } catch (e) {
          }
        },
        getConnected(url, connectConfig) {
          this.config = connectConfig ||
          {
            "login": import.meta.env.VITE_RABBIT_LOGIN,
            "passcode": import.meta.env.VITE_RABBIT_PASSCODE,
            "host": import.meta.env.VITE_RABBIT_HOST
          };
          this.client = Stomp.client(url);
          this.client.__proto__.debug = () => { };
          return this;
        },
        connect(successCb, failCb) {
          this.client.connect(this.config, successCb || this.onConnected, failCb || this.onFailed);
          return this;
        },
        onConnected(frame) {
          emitter.emit("ws-connect-success", frame);
          this.connectStatus = 1;
          return this;
        },
        sub(list, onMsg) {
          let self = this;
          list.forEach(element => {
            let {
              exchange, rk
            } = element;
            this.client.subscribe("/exchange/" + exchange + "/" + rk, onMsg || function (frame) {
              emitter.emit(self.msgEvtPrefix + rk, frame.body);
            });
            if (self.subList[exchange]) {
              self.subList[exchange].push(rk);
              self.isOk(null, 1);
              return;
            }
            self.subList[exchange] = [rk];
          });
          self.isOk(null, 1);
        },
        subscribe(exchange, rk, onMsg) {
          let self = this.isOk();
          if (this.connectStatus) {
            return this.sub([{ exchange, rk }], onMsg);
          }
          emitter.on("ws-connect-success", () => {
            self.sub([{ exchange, rk }], onMsg);
          });
          return this;
        },
        subscribeList(list, onMsg) {
          let self = this.isOk();
          if (this.connectStatus) {
            this.sub(list, onMsg);
            return this;
          }
          emitter.on("ws-connect-success", () => {
            self.sub(list, onMsg);
          });
          return this;
        },
        isOk(cb, ok) {
          if (!cb) {
            this.status = ok;
            return this;
          }
          let self = this, ck = setInterval(() => {
            if (self.status) {
              try {
                cb(self.subList);
              } catch (e) {
                console.error(e);
              }
              clearInterval(ck);
            }
          }, 100)
          return this;
        },
        onFailed(frame) {
          emitter.emit("ws-connect-fail", frame);
          this.connectStatus = 0;
          return this;
        }
      }
    })(this.instance)
  }
}